import CommonHeader from '@/components/common-header/index'
import UpdateEmployeeInfo from '@/apis/UpdateEmployeeInfo'

export default {
  name: 'EditUserinfo',
  components: {
    CommonHeader,
  },
  data() {
    return {
      active: 0,
      userInfo: {
        userName: '',
        sex: '',
        email: '',
        IDCard: '',
        graduationSchool: '',
        birthday: '',
      },
      ifReadonly: true,
      confirmInfo: '修改/填写个人信息',
      ifSexShow: false,
      sexList: ['男', '女'],
      ifBirthdayShow: false,
      datePicker: {
        minDate: new Date(1950, 0, 1).getTime(),
        maxDate: new Date().getTime(),
        currentDate: new Date(2000, 0, 1).getTime(),
      },
      // 岗位信息
      postInfoList: [
        {
          id: 1,
          value: '',
          label: '入职时间',
        },
        {
          id: 2,
          value: '',
          label: '部门',
        },
        {
          id: 3,
          value: '',
          label: '工作邮箱',
        },
        {
          id: 4,
          value: '',
          label: '岗位',
        },
        {
          id: 5,
          value: '',
          label: '工号',
        },
        {
          id: 6,
          value: '',
          label: '聘用形式',
        },
        {
          id: 7,
          value: '',
          label: '转正日期',
        },
        {
          id: 8,
          value: '',
          label: '转正状态',
        },
        {
          id: 9,
          value: '',
          label: '转正评价',
        },
        {
          id: 10,
          value: '',
          label: '职级',
        },
        {
          id: 11,
          value: '',
          label: '汇报对象',
        },
        {
          id: 12,
          value: '',
          label: '调整司龄',
        },
        {
          id: 13,
          value: '',
          label: '司龄',
        },
        {
          id: 14,
          value: '',
          label: '工龄',
        },
        {
          id: 15,
          value: '',
          label: '纳税城市',
        },
        {
          id: 16,
          value: '',
          label: '合同开始',
        },
        {
          id: 17,
          value: '',
          label: '合同到期',
        },
      ],
      // 岗位对象映射
      postInfoLink: {
        entryTime: 0,
        department: 1,
        workEmail: 2,
        job: 3,
        jobNumber: 4,
        hireForm: 5,
        positiveTime: 6,
        positiveState: 7,
        positiveEvaluation: 8,
        rank: 9,
        reportingObject: 10,
        adjustAge: 11,
        timeInCompany: 12,
        timeInWork: 13,
        taxCity: 14,
        contractStartTime: 15,
        contractExpiresTime: 16,
      },
    }
  },
  methods: {
    // 修改个人信息
    modifyUserinfo() {
      const _this = this
      if (this.confirmInfo === '修改/填写个人信息') {
        this.$dialog
          .confirm({
            context: this,
            title: '修改/填写个人信息',
            message: '确定要修改或者填写你的个人信息么?',
          })
          .then(() => {
            this.ifReadonly = false
            this.confirmInfo = '保存个人信息'
          })
          .catch(() => {
            // on cancel
          })
      } else {
        this.$dialog
          .confirm({
            context: this,
            title: '保存个人信息',
            message: '确定要保存你修改的个人信息么?',
          })
          .then(() => {
            // 进行表单判断
            if (
              this.userInfo.userName === '' ||
              this.userInfo.sex === '' ||
              this.userInfo.email === '' ||
              this.userInfo.IDCard === '' ||
              this.userInfo.graduationSchool === '' ||
              this.userInfo.birthday === ''
            ) {
              this.$toast('您有没有填写的个人信息')
              return
            }
            this.ifReadonly = true
            this.confirmInfo = '修改/填写个人信息'
            let submitObject = {
              jobNumber: this.$store.state.userInfo.jobNumber,
            }

            this.userInfo.userName !== this.$store.state.userInfo.name
              ? (submitObject['name'] = this.userInfo.userName)
              : null
            ;(this.userInfo.sex === '男' ? '0' : '1') !==
            this.$store.state.userInfo.sex
              ? (submitObject['sex'] = this.userInfo.sex === '男' ? '0' : '1')
              : null

            this.userInfo.IDCard !== this.$store.state.userInfo.idCardNumber
              ? (submitObject['idCardNumber'] = this.userInfo.IDCard)
              : null

            this.userInfo.birthday !== this.$store.state.userInfo.birthday
              ? (submitObject['birthday'] = this.userInfo.birthday)
              : null

            this.userInfo.graduationSchool !==
            this.$store.state.userInfo.graduationSchool
              ? (submitObject[
                  'graduationSchool'
                ] = this.userInfo.graduationSchool)
              : null

            this.userInfo.email !== this.$store.state.userInfo.email
              ? (submitObject['email'] = this.userInfo.email)
              : null
            // 调用修改个人信息接口
            UpdateEmployeeInfo(submitObject, this.$store.state.authorization)
              .then((res) => {
                // console.log(res)
                if (res.data.flag === true) {
                  this.$toast('保存个人信息成功')
                  // 更新vuex中的信息
                  this.$store.commit('initUserInfo', res.data.data)
                } else {
                  this.$toast('保存个人信息失败')
                  this.userInfo.userName = this.$store.state.userInfo.name
                  this.userInfo.sex =
                    this.$store.state.userInfo.sex === '0' ? '男' : '女'
                  this.userInfo.email = this.$store.state.userInfo.email
                  this.userInfo.IDCard = this.$store.state.userInfo.idCardNumber
                  this.userInfo.birthday = this.$store.state.userInfo.birthday
                  this.userInfo.graduationSchool = this.$store.state.userInfo.graduationSchool
                }
              })
              .catch((err) => {
                console.log(err)
              })
          })
          .catch(() => {
            // on cancel
          })
      }
    },
    showSex() {
      if (this.ifReadonly === false) {
        this.ifSexShow = true
      }
    },
    closeSex() {
      this.ifSexShow = false
    },
    sexConfirm(event) {
      const { picker, value, index } = event.detail
      this.userInfo.sex = value
      this.closeSex()
    },
    sexCancel() {
      this.closeSex()
    },
    showBirthday() {
      if (this.ifReadonly === false) {
        this.ifBirthdayShow = true
      }
    },
    closeBirthday() {
      this.ifBirthdayShow = false
    },
    birthdayConfirm(event) {
      const value = event.detail
      this.userInfo.birthday = getDate(value)
      this.closeBirthday()
      function getDate(time) {
        var date = new Date(time)
        var y = date.getFullYear()
        var m = date.getMonth() + 1
        if (m < 10) {
          m = '0' + m
        }
        var d = date.getDate()
        if (d < 10) {
          d = '0' + d
        }
        return y + '-' + m + '-' + d
      }
    },
    birthdayCancel() {
      this.closeBirthday()
    },
    formatter(type, value) {
      if (type === 'year') {
        return `${value}年`
      }
      if (type === 'month') {
        return `${value}月`
      }
      return `${value}日`
    },
  },
  mounted() {
    const error = {
      none: 'no error',
      user: '个人信息获取失败',
      post: '岗位信息获取失败',
      all: '数据获取失败',
    }
    let state = error['none']
    // 获取个人信息
    if (this.$store.state.userInfo) {
      this.userInfo.userName = this.$store.state.userInfo.name
      this.userInfo.sex = this.$store.state.userInfo.sex === '0' ? '男' : '女'
      this.userInfo.email = this.$store.state.userInfo.email
      this.userInfo.IDCard = this.$store.state.userInfo.idCardNumber
      this.userInfo.birthday = this.$store.state.userInfo.birthday
      this.userInfo.graduationSchool = this.$store.state.userInfo.graduationSchool
    } else {
      state = error['user']
    }
    // 获取岗位信息
    if (this.$store.state.postInfo) {
      // 入职时间
      this.postInfoList[
        this.postInfoLink.entryTime
      ].value = this.$store.state.postInfo.entryTime
      // 部门
      this.postInfoList[
        this.postInfoLink.department
      ].value = this.$store.state.postInfo.department
      // 工作邮箱
      this.postInfoList[
        this.postInfoLink.workEmail
      ].value = this.$store.state.postInfo.workEmail
      // 岗位
      this.postInfoList[
        this.postInfoLink.job
      ].value = this.$store.state.postInfo.job
      // 工号
      this.postInfoList[
        this.postInfoLink.jobNumber
      ].value = this.$store.state.postInfo.jobNumber
      // 聘用形式
      this.postInfoList[
        this.postInfoLink.hireForm
      ].value = this.$store.state.postInfo.hireForm
      // 转正日期
      this.postInfoList[
        this.postInfoLink.positiveTime
      ].value = this.$store.state.postInfo.positiveTime
      // 转正状态
      this.postInfoList[
        this.postInfoLink.positiveState
      ].value = this.$store.state.postInfo.positiveState
      // 转正评价
      this.postInfoList[
        this.postInfoLink.positiveEvaluation
      ].value = this.$store.state.postInfo.positiveEvaluation
      // 职级
      this.postInfoList[
        this.postInfoLink.rank
      ].value = this.$store.state.postInfo.rank
      // 汇报对象
      this.postInfoList[
        this.postInfoLink.reportingObject
      ].value = this.$store.state.postInfo.reportingObject
      // 调整司龄
      this.postInfoList[
        this.postInfoLink.adjustAge
      ].value = this.$store.state.postInfo.adjustAge
      // 司龄
      this.postInfoList[
        this.postInfoLink.timeInCompany
      ].value = this.$store.state.postInfo.timeInCompany
      // 工龄
      this.postInfoList[
        this.postInfoLink.timeInWork
      ].value = this.$store.state.postInfo.timeInWork
      // 纳税城市
      this.postInfoList[
        this.postInfoLink.taxCity
      ].value = this.$store.state.postInfo.taxCity
      // 合同开始
      this.postInfoList[
        this.postInfoLink.contractStartTime
      ].value = this.$store.state.postInfo.contractStartTime
      // 合同结束
      this.postInfoList[
        this.postInfoLink.contractExpiresTime
      ].value = this.$store.state.postInfo.contractExpiresTime
    } else if (state === error['user']) {
      state = error['all']
    } else {
      state = error['post']
    }
    // 错误信息提示
    if (state !== error['none']) {
      this.$toast(state)
    }
  },
}
